package com.zh.seckill.rabbitmq;


import cn.hutool.json.JSONUtil;
import com.zh.seckill.pojo.SeckillMessage;
import com.zh.seckill.pojo.User;
import com.zh.seckill.service.GoodsService;
import com.zh.seckill.service.OrderService;
import com.zh.seckill.vo.GoodsVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
@Slf4j
public class MQReceiverMessage {

    @Resource
    private GoodsService goodsService;

    @Resource
    private OrderService orderService;


    @RabbitListener(queues = "seckillQueue")
    public void secKillReceive(String message) {
        log.info("receive message:" + message);

        SeckillMessage seckillMessage = JSONUtil.toBean(message, SeckillMessage.class);
        //秒杀用户对象
        User user = seckillMessage.getUser();
        //秒杀的商品id
        Long goodsId = seckillMessage.getGoodsId();
        //通过商品id,得到对应的GoodsVo
        GoodsVo goodsVo = goodsService.findGoodsVoByGoodsId(goodsId);

        //下单操作
        orderService.seckill(user, goodsVo);


    }
}
